﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using dbPoll_Application.Models;

namespace dbPoll_Application.Controllers
{
    public class ScreenController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [Authorize(Roles = "Poll Master")]
        public ActionResult KeypadTest(int pollID, String show)
        {
            var poll = PollRepository.GetPollByID(pollID);
            ViewData["receiverChannel"] = poll.receiverChannel;
            ViewData["pollID"] = pollID;
            ViewData["show"] = show;

            return View(poll.participants);
        }

        [Authorize(Roles = "Poll Master")]
        public ActionResult Question(int pollID, int questionNumber)
        {
            var poll = PollRepository.GetPollByID(pollID);
            var questions = poll.questions.Where(m => m.deletionTime == null);
            var question = questions.ElementAt(questionNumber - 1);
            if (poll.isTestPoll)
            {
                ViewData["pollID"] = poll.linkedPoll.pollID;
            }
            else
            {
                ViewData["pollID"] = pollID;
            }
            ViewData["participants"] = poll.participants;
            ViewData["show"] = question.responseGrid.show;

            return View(question);
        }

        [Authorize(Roles = "Poll Master")]
        public int GetResponseCount(int questionID)
        {
            var question = QuestionRepository.GetQuestionByID<Question>(questionID);
            var poll = PollRepository.GetPollContainingQuestion(question);
            return question.responded.Count;
        }
    }
}
